# File used to convert date form a string into a python date representation

from datetime import datetime
import time
import email.utils

def dateConverter(strDate):
    # convert a date from a string function to a datetime object
    # tries to use the emails.utils parser for dates
    strDate = strDate.replace("Date:","")
    strDate = strDate.replace("date:","")
    strDate = strDate.strip()
    try:
        date = datetime.fromtimestamp(email.utils.mktime_tz(email.utils.parsedate_tz(strDate)))
        if date is None:
            return dateconverterDaynameDayMonthYear(strDate)
        else:
            return date
    except ValueError:
        print strDate
        return dateconverterDaynameDayMonthYear(strDate)

def dateconverterDaynameDayMonthYear(strDate):
    # convert a string form the format "ddd, DD MMM YYYY ..." into a date representation 
    strDate = strDate.replace("Date:","")
    strDate = strDate.replace("date:","")
    strDate = strDate.strip()

    if not(strDate.find("+") == -1):
        strDate=strDate.split("+")[0]
    if not(strDate.find("-") == -1):
        strDate=strDate.split("-")[0]
    strDate = strDate.strip()

    try:
        return datetime.strptime(strDate, '%a, %d %b %Y %H:%M:%S')
    except ValueError:
        return dateconverterMonthDayCommaYear(strDate)


def dateconverterMonthDayCommaYear(strDate):
    # convert a string form the format "MMM DD, YYYY ..." into a date representation 
    strDate = strDate.replace("Date:","")
    strDate = strDate.replace("date:","")
    strDate = strDate.strip()

    #strDate = strDate.replace(" AM", " am")
    #strDate = strDate.replace(" PM", " pm")
    
    try:
        return datetime.strptime(strDate, '%b %d, %Y %I:%M %p')
    except ValueError:
        return dateconvertDaynameMonthDay(strDate)

def dateconvertDaynameMonthDay(strDate):
    # convert a string from the format like 'Thu Sep  7 20:38:20 2006'
    strDate = strDate.replace("Date:","")
    strDate = strDate.replace("date:","")
    strDate = strDate.strip()

    try:
        return datetime.strptime(strDate, '%a %b  %d %H:%M:%S %Y')
    except ValueError:
        dateconvertDayMonthYear(strDate)

def dateconvertDayMonthYear(strDate):
    # convert a string from the format like '29 Jan 2007 04:59:37'
    strDate = strDate.replace("Date:","")
    strDate = strDate.replace("date:","")
    strDate = strDate.strip()
    
    try: 
        return datetime.strptime(strDate, '%d %b %Y %H:%M:%S')
    except ValueError:
        dateconvertDaynameCommaDayMonthYearEtc(strDate)

def dateconvertDaynameCommaDayMonthYearEtc(strDate):
    # convert a string from the format like 'Wed, 28 Mar 2007 07:59:06 GMT'
    strDate = strDate.replace("Date:","")
    strDate = strDate.replace("date:","")
    strDate = strDate.strip()

    s = strDate[0:len(strDate)-4]

    try:
        return datetime.strptime(s, '%a, %d %b %Y %H:%M:%S')
    except ValueError:
        dateconverterDayfullnameCommaMonthDayCommaYear(strDate)
        
def dateconverterDayfullnameCommaMonthDayCommaYear(strDate):
    # convert a string from the format like 'Friday, Sep 14, 2007 11:57 am'
    strDate = strDate.replace("Date:","")
    strDate = strDate.replace("date:","")
    strDate = strDate.strip()

    try:
        return datetime.strptime(strDate, '%A, %b %d, %Y %I:%M %p')
    except ValueError:
        dateconverterDayfullnameCommaMonthfullDayYear(strDate)

def dateconverterDayfullnameCommaMonthfullDayYear(strDate):
    # convert a string from the format like 'Monday, June 16, 2008, 9:21 AM'
    strDate = strDate.replace("Date:","")
    strDate = strDate.replace("date:","")
    strDate = strDate.strip()
    
    try:
        return datetime.strptime(strDate, '%A, %B %d, %Y, %I:%M %p')
    except ValueError:
        dateconverterDaynameCommaMonthDayYearAt(strDate)
        
def dateconverterDaynameCommaMonthDayYearAt(strDate):
    # convert a string from the format like 'Thu, Jun 19, 2008 at 8:53 AM'
    strDate = strDate.replace("Date:","")
    strDate = strDate.replace("date:","")
    strDate = strDate.strip()
    
    try:
        return datetime.strptime(strDate, '%a, %b %d, %Y at %I:%M %p')
    except ValueError:
        dateconverterDaynameCommaDayMonthYear2(strDate)
        
def dateconverterDaynameCommaDayMonthYear2(strDate):
    # convert a string from the format like 'Tue, 28 Jan 03 14:27:13'
    strDate = strDate.replace("Date:","")
    strDate = strDate.replace("date:","")
    strDate = strDate.strip()
    
    try:
        return datetime.strptime(strDate, '%a, %d %b %y %H:%M:%S')
    except ValueError:
        dateconverterDaynameCommaDayMonthYearEtc(strDate)
        
def dateconverterDaynameCommaDayMonthYearEtc(strDate):
    # convert a string from the format like 'Tue, 30 Sep 2003 09:12:45 CEST'
    
    strDate = strDate.replace("Date:","")
    strDate = strDate.replace("date:","")
    strDate = strDate.strip()
    
    s = strDate[0:len(strDate)-4]
    try:
        return datetime.strptime(s, '%a, %d %b %Y %H:%M:%S')
    except:
        print "Error: "+str(strDate)
        return None